import {Injectable} from '@angular/core';
import {LoadingController} from 'ionic-angular';
// import   $  from 'jquery';
import { Http, Response } from '@angular/http';
// import { resolveDefinition } from '@angular/core/src/view/util';
declare let Wechat;

@Injectable()
export class Share{
    regsID:string;
    constructor(public loadingCtrl:LoadingController,public http:Http) {
  
    }
    weChatAuth() {

        let loading = this.loadingCtrl.create({
            
            content: "跳转微信登录中...",//loading框显示的内容
            
            dismissOnPageChange: true, // 是否在切换页面之后关闭loading框
            
            showBackdrop: true  //是否显示遮罩层
            // $.get('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + appid + '&secret=' + secret + '&code=' + response.code + '&grant_type=authorization_code', function (accessTokenResponse) {
                //     alert('第二次'+JSON.stringify(accessTokenResponse));
                //       // you should save access token response somewhere, e.g. cookies, local storage, etc.
                //       var accessToken = accessTokenResponse.access_token;
                //       var openId = accessTokenResponse.openid;
                //       // get user information
                //       $.get('https://api.weixin.qq.com/sns/userinfo?access_token=' + accessToken + '&openid=' + openId + '&lang=zh_CN', function (userInfoResponse) {
                //           console.log(userInfoResponse);
                //       });
                //   });
        });
        
        loading.present();
        
        try {
        
            let scope = "snsapi_userinfo",
            
            state = "_" + (+new Date());
            
            Wechat.auth(scope, state, (response) => {
                let url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx5d1feb4264ef22da&secret=95ad2ee46206ff1b01a00e5caefa7284&code='+response.code + '&grant_type=authorization_code'
                this.http.request(url).subscribe((res)=>{
                    alert('全部数据'+res);
                },(err)=>{
                    alert('错误信息'+err)
                });
                console.log('获取code'+response.code);
                console.log('地址'+url);
            
            }, (reason) => {
            
                alert("Failed: " + reason);
            
            });
            
        } catch (error) {
        
            console.log(error);
        
        } finally {
        
            loading.dismiss();
        
        }
        
    }
        
        
}